package com.loooooo.pm2024.modules.pm.controller;


import com.loooooo.pm2024.data.Result;
import com.loooooo.pm2024.modules.pm.model.Issue;
import com.loooooo.pm2024.modules.pm.model.IssueComment;
import com.loooooo.pm2024.modules.pm.selector.IssueCommentSelector;
import com.loooooo.pm2024.modules.pm.selector.IssueSelector;
import com.loooooo.pm2024.modules.pm.service.IssueCommentService;
import com.loooooo.pm2024.modules.pm.service.IssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/pm/comment")
public class CommentController {
    @Autowired
    IssueCommentService commentService;

    @RequestMapping("/get")
    String get(@RequestParam(value="id",required = false)Long id) {
        return "pm/pm.comment.get";
    }
    @PostMapping(value = "/get", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result get(@RequestParam(value="id" )Long id,@RequestBody(required = false) IssueCommentSelector selector) {

        return Result.success(commentService.get(id,selector)).set("now", System.currentTimeMillis());

    }

    @RequestMapping("/list")
    String listGet() {
        return "pm/pm.comment.list";
    }

    @PostMapping(value = "/list", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result listPost(@RequestBody(required = false) IssueCommentSelector selector) {
        if (selector == null) selector = new IssueCommentSelector();

        if (selector.getPageSize() != null) {
            if (selector.getPageNo() == null || selector.getPageNo() < 1) {
                selector.setPageNo(1);
            }
            Result result = new Result();
            int count = commentService.count(selector);
            result.set("total", count);
            if (count > (selector.getPageSize() * (selector.getPageNo() - 1))) {
                result.setData(commentService.list(selector));
            }
            return result.set("now", System.currentTimeMillis());
        }
        return Result.success(commentService.list(selector)).set("now", System.currentTimeMillis());

    }

    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result update(@RequestBody IssueComment comment) {
        int r = commentService.update(comment);
        return Result.success(comment).set("affect", r).set("now", System.currentTimeMillis());
    }
    @PostMapping(value = "/like" )
    @ResponseBody
    Result update(@RequestParam(value="id") Long commentId) {
        int r = commentService.like(commentId);
        return Result.success(r).set("affect", r).set("now", System.currentTimeMillis());

    }
}
